<template>
	<view>
		<view>
			<button @click="scanCode()">扫码</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				sessionId:'',
				token:'',
				httpUrl:'',
				username:''
			}
		},
		methods: {
			scanCode(){
			    let _this=this;
				uni.scanCode({
				    onlyFromCamera: true,
				    success: function (res) {
				        console.log('条码类型：' + res.scanType);
				        console.log('条码内容：' + res.result);
						_this.sessionId=res.result;
						uni.getStorage({
							key:'info',
							success: (resStore)=>{
								let infoJson=JSON.parse(resStore.data);
								console.log(infoJson);
								_this.token=infoJson.token;
								_this.httpUrl=infoJson.httpUrl;
								console.log(_this.token,_this.httpUrl);
								_this.mobileScanOk();
							}
						});
				    }
				});
			},
			mobileScanOk(){
				let requestUrl=this.httpUrl+'/user/mobileScanOk'
				uni.request({
					method:'POST',
				    url: requestUrl,
				    data: {
				        sessionId: this.sessionId,
						token: this.token
				    },
					header:{
						"Content-Type": "application/x-www-form-urlencoded"
					},
				    success: (res) => {
						let resultData=res.data;
						console.log(resultData);
						if(resultData.code==0){
							
							let status=resultData.data.status;
							if(status==1){ //登录成功
							    this.username=resultData.data.username;
								uni.navigateTo({
									url: `./confirm?token=${this.token}&sessionId=${this.sessionId}&httpUrl=${this.httpUrl}&username=${this.username}`
								});
							}else{
								uni.showToast({
									title: resultData.msg,
									duration: 2000
								});
							}
						}else{
							uni.showToast({
								title: resultData.msg,
								duration: 2000
							});
						}

				    }
				});
			}
			
		}
	}
</script>

<style>

</style>
